/**
 * Created by Administrator on 2017/2/16 0016.
 */
//var basePath = "http://127.0.0.1:8080";
var basePath = "";
// var basePath = "http://127.0.0.1:8080";
// var basePath="http://192.168.12.112:8080";
//var basePath="http://192.168.0.106:8080";
function AjaxSubmit(_type, _data, _url, _method, _extraData) {//调用ajax
    $.ajax({
        type: _type,
        url: _url,
        data: _data,
        contenType: "application/json",
        dataType: "json",
        success: function (msg) {
            if (_extraData != null) {
                _method(msg, _extraData, 0);
            } else {
                _method(msg);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            // alert(XMLHttpRequest.responseText);
        }
    });
}//AJAX
function AjaxSubmit2(_type, _data, _url, _method, _extraData) {//调用ajax
    $.ajax({
        type: _type,
        url: _url,
        data: _data,
        contenType: "application/json",
        dataType: "json",
        async: false,
        success: function (msg) {
            if (_extraData != null) {
                _method(msg, _extraData);
            } else {
                _method(msg);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            // alert(XMLHttpRequest.responseText);
        }
    });
}//AJAX
function phoneMath(phone) {//验证手机号码
    if (phone.match(/^(((1[0-9][0-9]{1})|159|153)+\d{8})$/))//判断手机号的格式
    {
        return true;
    }
    else return false;
}//手机号判断
function name(name) {
    if (name.match(/^([\u4e00-\u9fa5]{1,20}|[a-zA-Z\.\s]{1,20})$/)) {
        return true
    }
    else {
        return false;
    }
}//判断姓名
function wrong(msg) {
    //登录按钮不可点
    $("html,body").animate({scrollTop: 0}, 500);
    $("input").attr("disabled", true);
    $("header").animate({opacity: 0.5}, 500);
    $("section").animate({opacity: 0.5}, 500);
    $("footer").animate({opacity: 0.5}, 500);
    var promptDiv = $('<div class="mA wd60 texC av-wrong" style="opacity:1 !important;"><div class="av-wrongT" style="opacity:1 !important;z-index:5000">温馨提示</div><div>' + msg + '</div><div class="av-close">关闭</div></div>');
    $("body").append(promptDiv);
    $(".av-close").click(function () {
        $("header").animate({opacity: 1}, 500);
        $("section").animate({opacity: 1}, 500);
        $("footer").animate({opacity: 1}, 500);
        $(".av-wrong").remove();
        $("input").removeAttr("disabled");
    })
}//提示框提醒
function i_close() {
    window.open("", "_self").close()
}//关闭当前页
function i_back() {
    if (history.length == 0) {
        openmodal("没有上一页");
    }
    else {
        history.go(-1);
        console.log(1)
    }
}//返回上一页
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return r[2];
    return null;
}//获取url参数
function login() {
    var value = JSON.parse(sessionStorage.getItem("token"));
    if (value == "" || value == undefined || value == null) {
        openmodal("请您进行登录!");
        setTimeout(function () {
            self.location.href = "login.html";
        }, 1500)
    }
}//验证登录
function login1() {
    var value = JSON.parse(sessionStorage.getItem("token"));
    var Coode = GetQueryString("code");
    if (Coode == "" || Coode == undefined || Coode == null) {
        if (value == "" || value == undefined || value == null) {
            openmodal("请您先登录");
            setTimeout(function () {
                self.location.href = "login.html";
            }, 1500)
        }
    }
    else {
        var wxData = '{"code":"' + Coode + '"}';
        AjaxSubmit("get", JSON.parse(wxData), basePath + "ydLogin/WxLogin", WxLogin_fun);
        function WxLogin_fun(wxCode) {
            console.log(wxCode)
            if (wxCode.result.code == "200") {
                sessionStorage.setItem("token", JSON.stringify(wxCode.dataPacket.token));
                active();
            }
            else if (wxCode.result.code == "3201") {
                openmodal("您未登陆");
                setTimeout(function () {
                    sessionStorage.setItem("personalCenter", JSON.stringify("pc"));
                    sessionStorage.setItem("code", JSON.stringify(Coode));
                    self.location.href = "login.html";
                }, 1500)
            }
            else if (wxCode.result.code == "300") {
                openmodal("对不起，查询错误");
            }
            else if (wxCode.result.code == "3202") {
                self.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxab851ef6bc481bd2&redirect_uri=http://nb.3renhe.net/WxWeb/html/personalCenter.html&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
            }
            else {
                openmodal("对不起，查询错误!");
            }
        }
    }

}//验证登录
function isWeiXin() {
    var ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
        return true;
    } else {
        return false;
    }
}//判断是否用微信内置浏览器打开
function fixed() {
    $("input").on("click", function () {
        if ($(this).attr("type") == 'text' || $(this).attr("type") == "tel") {
            $('.av-other').addClass("hide");
        }
    })
    $("input").on("blur", function () {
        $('.av-other').removeClass("hide");
    })
    //if($(input).attr("type")=='text'||$(input).attr("type")=="tel")
    //{
    //    $('.av-other').addClass("hide");
    //}
    //        //$('.av-other').css({'position':'static'})
    //if($(input).attr("type")=='text'||$(input).attr("type")=="tel")
    //{//$('.av-other').css({'position':'fixed'})
    //
    //}
}//手机端input的虚拟键盘和position：fixed冲突的解决方法
function fixedTextarea() {
    $("textarea").on("click", function () {
        $('.av-other').addClass("hide");
    })
    $("textarea").on("blur", function () {
        $('.av-other').removeClass("hide");
    })
}
function loginUser() {
    openmodal("请重新登录");
    sessionStorage.removeItem("token");
    setTimeout(function () {
        self.location.href = "login.html";
    }, 1500)
}
function inputChecked(input) {
    if ($(input).attr("checked") == true) {
        $(input).attr("checked", false)
    }
    else {
        $(input).attr("checked", true);
    }
}
function img_click(imgClick) {
    var imgSrc = $(imgClick).attr("src");
    var bodyHeight = $(document.body).outerHeight(true);
    var imgHeight = document.documentElement.clientHeight;
    var imgWidth = document.documentElement.clientWidth;
    console.log(imgHeight);
    var bigImg = $('<div class="bigImg" onclick="img_clickOut(this)"> <div class="page"> ' +
        '<div id="slider"><ul> <li style="display:block"> <div class="pinch-zoom"><img src="' + imgSrc + '" class="bigImgs"/></div></li> </ul></div></div></div>');
    $("body").append(bigImg);
    $('div.pinch-zoom').each(function () {
        new RTP.PinchZoom($(this), {});
    });
    var img = new Image();
    img.src = $(".bigImgs").attr("src");
    var ImgWidth = img.width;
    var ImgHeight = img.height;
    console.log(bodyHeight)
    //var minHeight=$("body").scrollTop();
    //console.log(ImgWidth)
    if (imgHeight < bodyHeight) {
        $(".bigImg").css("height", bodyHeight + 'px');
    }
    else {
        $(".bigImg").css("height", imgHeight + 'px');
    }
    $(".bigImg").css("width", imgWidth + 'px');
    //console.log($(".bigImg").css("width").slice(0,length-2));
    //console.log(ImgWidth>$(".bigImg").css("width").slice(0,length-2))
    //console.log(ImgHeight>$(".bigImg").css("height").slice(0,length-2))
    console.log(window.screen.availHeight)
    //$(".bigImg").css("top",minHeight+'px');
    if (ImgHeight > $(".bigImg").css("height").slice(0, length - 2))//判断图片高度是否高于手机屏
    {
        $(".bigImgs").css("height", $(".bigImg").css("height"));
    }
    if (ImgWidth > $(".bigImg").css("width").slice(0, length - 2))//判断图片宽度是否宽于手机屏
    {
        $(".bigImgs").css("width", $(".bigImg").css("width"));
    }
    console.log($(".bigImgs").css("height"));
    var pageHeight = ($(".bigImg").css("height").slice(0, length - 2) - $(".bigImgs").css("height").slice(0, length - 2)) / 2;
    console.log(pageHeight)
    if (pageHeight > 0) {
        $(".pinch-zoom-container").css("top", pageHeight + "px");
    }
    $(".bigImg").fadeIn(1000);
}//图片点击放大
function img_clickOut(imgClickOut) {
    $(imgClickOut).fadeOut(1000);
    setTimeout(function () {
        $(imgClickOut).remove();
        $("html").removeAttr("style");
        $("body").removeAttr("style");
    }, 1000)
}//图片点击还原
function tdCount() {
    var count = 0;
    var length = $(".o-orderBtn").find("td").length;
    $(".o-orderBtn").find("td").each(function (i, td) {
        if ($(td).hasClass("disIB")) {
            count++
        }
    })
    var tdWidth = 80 / (count);
    $(".o-orderBtn").find("td").css("width", tdWidth + "%")
    console.log(count);
}//计算底部td宽度
function Android() {
    var u = navigator.userAgent;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    //var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    if (u.indexOf('Android') > -1 || u.indexOf('Adr') > -1) {
        return true;
    }
}//判断是否安卓手机
function banner() {
    $(".main_visual").hover(function () {
        $("#btn_prev,#btn_next").fadeIn();
    }, function () {
        $("#btn_prev,#btn_next").fadeOut();
    });
    $dragBln = false;
    $(".main_image").touchSlider({
        flexible: true,
        speed: 200,
        btn_prev: $("#btn_prev"),
        btn_next: $("#btn_next"),
        paging: $(".flicking_con a"),
        counter: function (e) {
            $(".flicking_con a").removeClass("on").eq(e.current - 1).addClass("on");
        }
    });
    $(".main_image").bind("mousedown", function () {
        $dragBln = false;
    });
    $(".main_image").bind("dragstart", function () {
        $dragBln = true;
    });
    $(".main_image a").click(function () {
        if ($dragBln) {
            return false;
        }
    });
    timer = setInterval(function () {
        $("#btn_next").click();
    }, 2000);
    $(".main_visual").hover(function () {
        clearInterval(timer);
    }, function () {
        timer = setInterval(function () {
            $("#btn_next").click();
        }, 2000);
    });
    $(".main_image").bind("touchstart", function () {
        clearInterval(timer);
    }).bind("touchend", function () {
        timer = setInterval(function () {
            $("#btn_next").click();
        }, 2000);
    });
}//轮播图
function footerNav() {
    $(".i-navTdP").click(function () {
        $(".i-navLiP").toggle();
    });
}//底部导航条
function threeProduct() {
    var data = '{"id":"1"}';
    AjaxSubmit("get", JSON.parse(data), basePath + "WxProduct/getProduct", getProductThree_fun);
}//底部导航条商品购买
function getProductThree_fun(result) {
    var product = eval(result.dataPacket.product);//product表
    var attr = eval(result.dataPacket.attr);//attr属性
    var priceList = eval(result.dataPacket.priceList);
    sessionStorage.setItem("ProductName", JSON.stringify(product.ProductName));
    if (product.IsAttributes == 1) {
        $.each(attr, function (i, obj) {
            if (product.Id == obj.ProductId) {
                $.each(obj.secAttr, function (j, obj2) {
                    $.each(priceList, function (z, obj3) {
                        if (obj2.Id == obj3.AttrIdList && obj3.AttrIdList == "23") {
                            sessionStorage.setItem("key", JSON.stringify(obj3));
                            self.location.href = "orderConfirmation.html";
                        }
                    })
                })
            }
        })
    }
}
function footerNavLi(data) {
    var orderType = $(data).attr("data-id");
    sessionStorage.setItem("orderType", JSON.stringify(orderType));
    self.location.href = "orderList.html"
}

// 数值转换成时间
function toHourMinute(minutes) {
    if (Math.floor(minutes / 3600) == 0) {
        return (parseInt((minutes % 3600) / 60) + "分钟" );
    } else {
        return (Math.floor(minutes / 3600) + "小时" + parseInt((minutes % 3600) / 60) + "分钟" );
    }
}